package com.tianhua.javs.core.engine.handler;

import com.tianhua.javs.client.bean.*;
import com.tianhua.javs.client.enums.BusinessKeyWardEnum;
import com.tianhua.javs.client.enums.SegmentSceneEnum;
import com.tianhua.javs.client.model.AbstractClassModel;
import com.tianhua.javs.core.constant.TagConstants;
import com.tianhua.javs.core.factory.ParamReferFactory;
import com.tianhua.javs.core.func.script.IBusinessFunc;
import com.tianhua.javs.core.parse.SegmentSpreadParseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Description:
 * date: 2022/2/24
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
public class DTOHandler extends AbstractElementHandler{
    @Autowired
    private ParamReferFactory paramReferFactory;

    @Autowired
    private SegmentSpreadParseService segmentSpreadParseService;

    @Resource(name = "dtoFunc")
    private IBusinessFunc dtoFunc;

    /**
     * 处理赋值场景
     * @param javsShell
     * @param keyWard
     * @param translateContextBean
     * @return
     */
    public TranslateResultBean handleTranslate(String javsShell, String keyWard, JavsTranslateContextBean translateContextBean, SegmentSceneEnum sceneEnum) {
        if(sceneEnum.getCode().equals(SegmentSceneEnum.MODEL_ASSIGN.getCode())){
            return dealModelAssign(javsShell, keyWard, translateContextBean);
        }
        return dealModelBeanAssign(javsShell, keyWard, translateContextBean);
    }

    private TranslateResultBean dealModelAssign(String javsShell, String keyWard, JavsTranslateContextBean translateContextBean){
        String varCode = getVarCodeFromKeyWard(keyWard);
        String className = translateContextBean.getCurrentScriptMethodBean().searchVarClassType(varCode);
        JavsModelBean javsModelBean = translateContextBean.getJavsModelBeanMap(BusinessKeyWardEnum.DTO.getKeyWardCode()).get(className);
        Map<String, JavsParamBean> javsVarMap = paramReferFactory.buildDTOJavsParamMap(varCode, javsModelBean, translateContextBean.getCurrentScriptMethodBean());
        TranslateResultBean translateResultBean = dtoFunc.translate(translateContextBean, keyWard, javsVarMap);
        if (translateResultBean != null) {

            ScriptVarMappingBean scriptVarMappingBean = translateResultBean.getMatchSegmentBean().getScriptVarMappingBean();
            if (scriptVarMappingBean != null) {
                String varName = javsShell.split(TagConstants.EQ)[0].trim();
                scriptVarMappingBean.setJavsVarCode(varName);
                String segment = translateResultBean.getSegment().trim();
                String javaVarName = varName;
                if (segment.contains(TagConstants.EQ)) {
                    javaVarName = segment.split(TagConstants.EQ)[0].split(" ")[1];
                }
                scriptVarMappingBean.setJavaCode(javaVarName);
                if (!StringUtils.isEmpty(scriptVarMappingBean.getVarClassName())) {
                    AbstractClassModel abstractClassModel = translateContextBean.getDomainModelBean().searchClass(null, scriptVarMappingBean.getVarClassName());
                    scriptVarMappingBean.setReferClassModel(abstractClassModel);
                }
                translateContextBean.getCurrentScriptMethodBean().addRunTimeVar(scriptVarMappingBean);
            }

            StringBuilder builder = new StringBuilder(segmentSpreadParseService.getBlankPrefix(javsShell));
            String javaAssignSegment = translateResultBean.getSegment().trim() + TagConstants.ROW_END;
            builder.append(javaAssignSegment);
            return TranslateResultBean.getInstance(builder.toString(), true);
        }
        return TranslateResultBean.getInstance(keyWard, false);
    }


    /**
     * 处理bean引用场景
     *
     * @param javsShell
     * @param keyWard
     * @param translateContextBean
     * @return
     */
    public TranslateResultBean dealModelBeanAssign(String javsShell, String keyWard, JavsTranslateContextBean translateContextBean) {
        String varCode = getVarCodeFromKeyWard(keyWard);
        String className = translateContextBean.getCurrentScriptMethodBean().searchVarClassType(varCode);
        JavsModelBean javsModelBean = translateContextBean.getJavsModelBeanMap(BusinessKeyWardEnum.DTO.getKeyWardCode()).get(className);
        Map<String, JavsParamBean> javsVarMap = paramReferFactory.buildDTOJavsParamMap(varCode, javsModelBean, translateContextBean.getCurrentScriptMethodBean());
        TranslateResultBean translateResultBean = dtoFunc.translate(translateContextBean, keyWard, javsVarMap);
        if (translateResultBean != null) {
            String javsVarName = javsShell.split(TagConstants.EQ)[0].trim();
            StringBuilder builder = new StringBuilder(segmentSpreadParseService.getBlankPrefix(javsShell));
            builder.append(translateResultBean.getSegmentClass());
            builder.append(TagConstants.BLANK_ONE);
            builder.append(javsVarName);
            builder.append(TagConstants.BLANK_ONE);
            builder.append(TagConstants.EQ);
            builder.append(TagConstants.BLANK_ONE);
            String javaAssignSegment = translateResultBean.getSegment().trim() + TagConstants.ROW_END;
            builder.append(javaAssignSegment);
            return TranslateResultBean.getInstance(builder.toString(), true);
        }
        return TranslateResultBean.getInstance(keyWard);
    }

}
